@echo off
echo ==========================
echo Carbon Planet - One-Click Start Tool
echo ==========================
echo.

echo [1/5] Creating data directories...
mkdir C:\data 2>nul
mkdir C:\data\db 2>nul

echo [2/5] Cleaning cache files...
if exist "node_modules\.cache" (
  rmdir /s /q "node_modules\.cache"
  echo Cache cleaned.
)

echo [3/5] Setting performance options...
set NODE_ENV=production
set NODE_OPTIONS=--max-old-space-size=2048

echo [4/5] Building application...
call npm run build
echo Build completed!
echo.

echo [5/5] Starting services...
start "MongoDB" "C:\Program Files\MongoDB\Server\8.0\bin\mongod.exe" --dbpath C:\data\db

echo Waiting for MongoDB to start (3 seconds)...
ping 127.0.0.1 -n 4 > nul

start "Carbon Planet Server" cmd /k "node --optimize_for_size --max_old_space_size=1024 server.js"

echo Waiting for server to start (2 seconds)...
ping 127.0.0.1 -n 3 > nul

echo.
echo ==========================
echo All services have been started!
echo Local access: http://localhost:3000
echo.
echo Other devices in the local network can access via:
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| findstr /c:"IPv4"') do (
    echo http:%%a:3000
    goto :found
)
:found
echo ==========================
echo.

start http://localhost:3000

echo Operation completed! You can close this window now.
pause 